global infile "DataMain.dta"
global outfile "$results\Figure 3"


* Create event dummies

use "$infile", clear

forvalues i = 26(-1)1 {
	gen eL`i' = 0
	replace eL`i' = 1 if weeksDD == 1 & cohort == 1 & week == -`i'
}
forvalues i = 0(1)33 {
	gen e`i' = 0
	replace e`i' = 1 if weeksDD == 1 & cohort == 1 & week == `i'
}
gen e34 = 0
replace e34 = 1 if weeksDD == 1 & cohort == 1 & week >= 34
forvalues i = 26(-1)1 {
	replace eL`i' = 1 if weeksDD == 2 & cohort == 2 & week2 == -`i'
}
forvalues i = 0(1)33 {
	replace e`i' = 1 if weeksDD == 2 & cohort == 2 & week2 == `i'
}
replace e34 = 1 if weeksDD == 2 & cohort == 2 & week2 >= 34
forvalues i = 26(-1)1 {
	replace eL`i' = 1 if weeksDD == 2 & cohort == 3 & week2 == -`i'
}
forvalues i = 0(1)33 {
	replace e`i' = 1 if weeksDD == 2 & cohort == 3 & week2 == `i'
}
replace e34 = 1 if weeksDD == 2 & cohort == 3 & week2 >= 34


* Regression estimates

reghdfe logwater eL26-eL6 eL4-eL1 e0-e34 if dataset == 1, absorb(datasethome datasetweek) cluster(home)
mat dd1 = r(table)
test eL26 eL25 eL24 eL23 eL22 eL21 eL20 eL19 eL18 eL17 eL16 eL15 ///
eL14 eL13 eL12 eL11 eL10 eL9 eL8 eL7 eL6
test eL4 eL3 eL2 eL1 

reghdfe logwater eL26-eL6 eL4-eL1 e0-e34 if dataset == 2, absorb(datasethome datasetweek) cluster(home)
mat dd2 = r(table)
test eL9 eL8 eL7 eL6
test eL4 eL3 eL2 eL1 

reghdfe logwater eL26-eL6 eL4-eL1 e0-e34 if dataset == 3, absorb(datasethome datasetweek) cluster(home)
mat dd3 = r(table)
test eL9 eL8 eL7 eL6
test eL4 eL3 eL2 eL1 


* Figure 3. Event estimates

clear
set obs 60
gen event = _n-27 if _n <= 21
replace event = _n-26 if _n >= 22
forvalues i = 1(1)3 {
	gen beta`i' = .
	gen betalow`i' = .
	gen betahigh`i' = .
	forvalues j = 1(1)60 {
		replace beta`i' = dd`i'[1,`j'] if _n == `j'
		replace betalow`i' = dd`i'[5,`j'] if _n == `j'
		replace betahigh`i' = dd`i'[6,`j'] if _n == `j'
	}
	replace beta`i' = . if beta`i' == 0
	replace betalow`i' = . if beta`i' == 0
	replace betahigh`i' = . if beta`i' == 0
	replace betalow`i' = -0.7 if betalow`i' <= -0.7
	replace betahigh`i' = 0.3 if betahigh`i' >= 0.3
}

set obs 61
replace event = -5 if _n == 61

forvalues i = 1(1)3 {
	replace beta`i' = 0 if event == -5
	replace betalow`i' = 0 if event == -5
	replace betahigh`i' = 0 if event == -5
	egen betaprem`i' = mean(beta`i') if event <= -6
	egen betaantm`i' = mean(beta`i') if event >= -4 & event <= -1 
	egen betapostm`i' = mean(beta`i') if event >= 0 & event <= 33
}
replace event = 37 if event == 34
replace betapostm1 = . if event >= 34
replace betaprem2 = . if event <= -10
replace betaprem3 = . if event <= -10

sort event

twoway (scatter beta1 event, msize(vsmall)) ///
(scatter beta1 event if event == -5, mc(navy)) ///
(rcap betahigh1 betalow1 event, lc(navy%50)) ///
(line betaprem1 event, lc(maroon) lw(thick)) ///
(line betaantm1 event, lc(maroon) lw(thick)) ///
(line betapostm1 event, lc(maroon) lw(thick)), ///
xli(-0.5, lp(dash) lc(forest_green)) ///
ti("A. Group 1 diff-in-diff event estimates") yti("Estimated log effect") xti("Event week in Period 1") ///
ysc(r(-0.7 0.35)) ylab(-0.6(0.2)0.2) xsc(r(-30 40)) xlab(-30(10)30) ///
leg(off) graphregion(color(white)) 
graph save "$results\TempA", replace

twoway (scatter beta2 event, msize(vsmall)) ///
(scatter beta2 event if event == -5, mc(navy)) ///
(rcap betahigh2 betalow2 e, lc(navy%50)) ///
(line betaprem2 event, lc(maroon) lw(thick)) ///
(line betaantm2 event, lc(maroon) lw(thick)) ///
(line betapostm2 event, lc(maroon) lw(thick)), ///
xli(-0.5, lp(dash) lc(forest_green)) ///
ti("B. Group 2 diff-in-diff event estimates") yti("Estimated log effect") xti("Event week in Period 2") ///
ysc(r(-0.7 0.35)) ylab(-0.6(0.2)0.2) xsc(r(-30 40)) xlab(-30(10)30 37 "34-191") ///
leg(off) graphregion(color(white)) 
graph save "$results\TempB", replace

twoway (scatter beta3 event, msize(vsmall)) ///
(scatter beta3 event if event == -5, mc(navy)) ///
(rcap betahigh3 betalow3 event, lc(navy%50)) ///
(line betaprem3 event, lc(maroon) lw(thick)) ///
(line betaantm3 event, lc(maroon) lw(thick)) ///
(line betapostm3 event, lc(maroon) lw(thick)), ///
xli(-0.5, lp(dash) lc(forest_green)) ///
ti("C. Group 3 diff-in-diff event estimates") yti("Estimated log effect") xti("Event week in Period 2") ///
ysc(r(-0.7 0.35)) ylab(-0.6(0.2)0.2) xsc(r(-30 40)) xlab(-30(10)30 37 "34-191") ///
leg(off) graphregion(color(white)) 
graph save "$results\TempC", replace

graph combine "$results\TempA" "$results\TempB" "$results\TempC", ///
rows(3) graphregion(color(white)) iscale(0.7) ysize(8)
graph save "$outfile", replace
